<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({ name: 'menu-create' })"
          >添加菜单</el-button
        >
      </div>
      <el-table :data="menus" style="width: 100%">
        <el-table-column label="编号" min-width="150" type="index">
        </el-table-column>
        <el-table-column prop="Name" label="菜单名称" min-width="150">
        </el-table-column>
        <el-table-column prop="Icon" label="前端图标" min-width="150">
        </el-table-column>
        <el-table-column prop="OrderSort" label="排序" min-width="150">
        </el-table-column>
        <el-table-column prop="Func" label="方法" min-width="150">
        </el-table-column>
        <el-table-column prop="Description" label="描述" min-width="150">
        </el-table-column>
        <el-table-column prop="MName" label="api路径" min-width="150">
        </el-table-column>
        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import Vue from 'vue'
import { getAllMenus, deleteMenu } from '@/services/system/menu'

export default Vue.extend({
  name: 'MenuIndex',
  data() {
    return {
      menus: [], // 菜单列表
      query: {
        page: 1,
        key: '',
      },
    }
  },

  created() {
    this.loadAllMenus()
  },

  methods: {
    async loadAllMenus() {
      const { data } = await getAllMenus(this.query)
      this.menus = data.response.data
    },

    handleEdit(item) {
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: item.id,
        },
      })
    },

    handleDelete(item) {
      this.$confirm('确认删除吗？', '删除提示', {})
        .then(async() => {
          // 确认执行这里
          // 请求删除操作
          const { data } = await deleteMenu(item.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadAllMenus() // 更新数据列表
          }
        })
        .catch((err) => {
          // 取消执行这里
          console.log(err)
          this.$message.info('已取消删除')
        })
    },
  },
})
</script>

<style lang="scss" scoped></style>
